﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using UnityEngine.Networking;

public class PauseAndStart : MonoBehaviour
{
    public GameObject audioTrack;
    string[] noteDetail;
    private int count;//三秒后继续的计时
    private float timeStart;
    private bool gameGoOn = false;
    public GameObject backButton;
    private AudioSource aud;
    // Start is called before the first frame update
    void Start()
    {
        if (GlobalSettings.Instance.isStarted == false)
        {
            audioTrack.GetComponent<AudioSource>().Stop();
            ReadText();
            GlobalSettings.Instance.noteSum = 0;
            GlobalSettings.Instance.combo = 0;
            GlobalSettings.Instance.pureNum = 0;
            GlobalSettings.Instance.farNum = 0;
            GlobalSettings.Instance.lostNum = 0;
            GlobalSettings.Instance.score = 0;
            for (int i = 1; i < GlobalSettings.Instance.notes.Length - 1; i++)
            {
                GlobalSettings.Instance.noteSum++;
                noteDetail = GlobalSettings.Instance.notes[i].Split(',');
                if (noteDetail.Length == 4)
                {
                    int holdTailSum = (int)((float.Parse(noteDetail[3]) - float.Parse(noteDetail[0])) * float.Parse(noteDetail[1]) / 60f); //计算长条除了头判以外的物量
                    if ((float)(holdTailSum + 1) - ((float.Parse(noteDetail[3]) - float.Parse(noteDetail[0])) * float.Parse(noteDetail[1]) / 60f) < 0.0011)//0.9989以上物量的算1物量 暂时用这种方式解决物量计算问题
                    {
                        holdTailSum++;
                    }
                    GlobalSettings.Instance.noteSum += holdTailSum;
                }
            }
            GlobalSettings.Instance.pureScore = 10000000f / (float)GlobalSettings.Instance.noteSum;
            GlobalSettings.Instance.farScore = 5000000f / (float)GlobalSettings.Instance.noteSum;
            //audioTrack.GetComponent<AudioSource>().clip = Resources.Load<AudioClip>("song/" + GlobalSettings.Instance.notes[0]);
#if UNITY_ANDROID || UNITY_EDITOR
            //string musicPath = Application.persistentDataPath + "/song/" + GlobalSettings.Instance.notes[0] + ".ogg";//安卓11以后data普通用户不root无权限自行写入 考虑换存储路径
            string musicPath = "/storage/emulated/0/TheFinalMugDemo/song/" + GlobalSettings.Instance.notes[0] + ".ogg";
#else
            string musicPath = Application.dataPath + "/Resources/song/" + GlobalSettings.Instance.notes[0] + ".ogg";
#endif
            aud = audioTrack.GetComponent<AudioSource>();
            StartCoroutine("LoadMusic", musicPath);
            //audioTrack.GetComponent<AudioSource>().Play();
            GlobalSettings.Instance.isStarted = true;
            GlobalSettings.Instance.sheetEnd = false;
            GlobalSettings.Instance.startTime = Time.time;
        }
    }


    // Update is called once per frame
    void Update()
    {
        if (GlobalSettings.Instance.isPaused == true && gameGoOn == true)
        {
            if (Time.realtimeSinceStartup > timeStart + count)
            {
                if (count == 4)
                {
                    Time.timeScale = 1;
                    audioTrack.GetComponent<AudioSource>().Play();
                    GetComponentInChildren<Text>().text = "Pause";
                    GlobalSettings.Instance.isPaused = false;
                    gameGoOn = false;
                }
                else
                {
                    GetComponentInChildren<Text>().text = (4 - count).ToString();
                    count++;
                }
            }
        }

    }
    public void pauseStart()
    {
        if (GlobalSettings.Instance.isPaused == false)
        {
            Time.timeScale = 0;
            audioTrack.GetComponent<AudioSource>().Pause();
            GetComponentInChildren<Text>().text = "start";
            GlobalSettings.Instance.isPaused = true;
            gameGoOn = false;
            backButton.SetActive(true);
        }
        else
        {
            backButton.SetActive(false);
            gameGoOn = true;
            timeStart = Time.realtimeSinceStartup;
            count = 1;
        }
    }
    private void ReadText()
    {
#if UNITY_ANDROID
        //string path = Application.persistentDataPath + "/sheet/" + GlobalSettings.Instance.songName;  //安卓11以后data普通用户不root无权限自行写入 考虑换存储路径
        string path = "/storage/emulated/0" + "/TheFinalMugDemo/sheet/" + GlobalSettings.Instance.songName;
#else
        string path = Application.dataPath + "/Resources/sheet/" + GlobalSettings.Instance.songName;
#endif
        GlobalSettings.Instance.notes = File.ReadAllLines(path);
    }

    //filepath 绝对路径 安卓sdcard 或者PC 绝对路径
    public IEnumerator LoadMusic(string filepath)
    {
        filepath = "file://" + filepath;
         using (var uwr = UnityWebRequestMultimedia.GetAudioClip(filepath, AudioType.UNKNOWN))
        {
            //不卡顿的2行代码
            ((DownloadHandlerAudioClip)uwr.downloadHandler).compressed = false;
            ((DownloadHandlerAudioClip)uwr.downloadHandler).streamAudio = true;
            yield return uwr.SendWebRequest();
            if (uwr.isNetworkError)
            {
                Debug.LogError(uwr.error);
            }
            else
            {
                AudioClip clip = DownloadHandlerAudioClip.GetContent(uwr);
                if (aud.clip != null) //释放上次的
                {
                    AudioClip.Destroy(aud.clip);
                }
                aud.clip = clip;
                aud.Play();//播放
            }
        }

    }
}